"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:checkuserDefine
 * @description
 * # checkuserDefine
 */
angular.module("jgappApp").directive("citySelect", function ($http) {
  return {
    scope: {
      param: "=param",
    },
    restrict: "AE",
    templateUrl: "views/userdefined/city_select.html",
    replace: true,
    link: function postLink(scope, element, attrs) {
      // 如果是审批页面只做展示
      $http({
        url: "./region.json",
        dataType: "json",
      }).then(function (response) {
        scope.data = response.data;
        scope.province=response.data.find(function(item){
          return item.name===scope.param.province
        });
        scope.city=scope.province.child.find(function(item){
          return item.name===scope.param.city
        });
        scope.county=scope.city.child.find(function(item){
          return item.name===scope.param.county
        });
      })
      scope.$watchGroup(["province", "city", "county"], function (newValue, oldValue) {
        var province = newValue[0], city = newValue[1], county = newValue[2];
        scope.param.province = province && province.name;
        scope.param.city = city && city.name;
        scope.param.county = county && county.name;
      });

    }
  };
});
